package com.example.game;

import android.util.Log;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.joints.MouseJoint;
import org.jbox2d.dynamics.joints.MouseJointDef;
import org.jbox2d.dynamics.World;
/**
 * Created by Lucas on 13/07/13.
 */
public class Spring {

    private MouseJoint _mouseJoint;
    private Body groundBody;
    private World _world;
    private final String TAG = "Spring";


    public Spring(World world){
        _world = world;
        _mouseJoint = null;
        groundBody = null;
        Log.d(TAG, "create Spring with values = null ");
    }

    public void update(Vec2 target){
        Log.d(TAG, "Update the target to :" + target.toString());
        if (_mouseJoint!= null){
            _mouseJoint.setTarget(target);
        }
    }

    public void bind(Vec2 touch, Body bodyBall){
        Log.d(TAG, "Bind the mousejoint to the body");
        MouseJointDef mjd = new MouseJointDef();
        groundBody = _world.createBody(new BodyDef());
        mjd.bodyA = groundBody;
        mjd.bodyB = bodyBall;
        mjd.target.set(touch);
        mjd.maxForce = 1000.0f*bodyBall.m_mass;
        mjd.frequencyHz = 5.0f;
        mjd.dampingRatio = 0.9f;
        _mouseJoint = (MouseJoint) _world.createJoint(mjd);
    }

    public void destroy(){
        if (_mouseJoint!= null){
            _world.destroyJoint(_mouseJoint);
            _world.destroyBody(groundBody);
            _mouseJoint = null;
            groundBody = null;
        }
        Log.d(TAG, "Action up, pointers null");
    }
}
